package com.zhengxing.jienci.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhengxing.jienci.user.service.IPointDetailService;
import com.zhengxing.jienci.web.context.SecurityContext;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.zhengxing.jienci.common.pojo.CustomizePage;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.user.domain.PointDetail;
import java.util.*;

/**
 * <p>
 * 积分明细 前端控制器
 * </p>
 *
 * @author yyh
 * @since 2020-12-02
 */
@Api("积分明细")
@RestController
@RequestMapping("/pointDetail")
public class PointDetailController {

	@Autowired
	private IPointDetailService iPointDetailService;

	/**
	 * 通过id查询
	 */
	@ApiOperation("详情查询")
	@GetMapping("/info/{id}")
	public PointDetail getById(@PathVariable(value = "id") String id) {
		return iPointDetailService.getById(id);
	}

	/**
	 * 新增
	 */
	@ApiOperation("新增操作")
	@PostMapping("/save")
	public SimpleResponse save(@RequestBody Map<String, String> param) {
		iPointDetailService.save(param.get("userId"), param.get("operation"));
		return SimpleResponse.success();
	}

	/**
	 * 通过id删除
	 */
	@ApiOperation("删除操作")
	@DeleteMapping("/delete/{id}")
	public SimpleResponse delete(@PathVariable(value = "id") String ids) {
		String[] idsStrs = ids.split(",");
		for (String id : idsStrs) {
			iPointDetailService.removeById(id);
		}
		return SimpleResponse.success();
	}

	/**
	 * 修改
	 */
	@ApiOperation("修改操作")
	@PutMapping("/update")
	public SimpleResponse updateById(@RequestBody PointDetail pointDetail) {
		iPointDetailService.updateById(pointDetail);
		return SimpleResponse.success();
	}

	/**
	 * 查询列表
	 */
	@ApiOperation("列表获取")
	@GetMapping("/list")
	public List<PointDetail> list(PointDetail pointDetail) {
		LambdaQueryWrapper<PointDetail> lambda = new QueryWrapper<PointDetail>().lambda();
		this.buildCondition(lambda, pointDetail);
		return iPointDetailService.list(lambda);
	}

	/**
	 * 分页查询
	 */
	@ApiOperation("分页展示")
	@GetMapping("/page")
	public IPage<PointDetail> page(CustomizePage page, PointDetail pointDetail) {
		String userId = SecurityContext.getUserId();
		pointDetail.setUserId(userId);
		Page<PointDetail> pages = new Page<PointDetail>(page.getPageNow(), page.getPageSize());
		LambdaQueryWrapper<PointDetail> lambda = new QueryWrapper<PointDetail>().lambda();
		this.buildCondition(lambda, pointDetail);
		IPage<PointDetail> resultPage = iPointDetailService.page(pages, lambda);
		return resultPage;
	}

	/**
	 * 构造查询条件
	 * 
	 * @param lambda
	 * @param param
	 */
	@SuppressWarnings("unchecked")
	private void buildCondition(LambdaQueryWrapper<PointDetail> lambda, PointDetail param) {
		if (!StringUtils.isEmpty(param.getId())) {
			lambda.eq(PointDetail::getId, param.getId());
		}
		if (!StringUtils.isEmpty(param.getOperation())) {
			lambda.eq(PointDetail::getOperation, param.getOperation());
		}
		if (!StringUtils.isEmpty(param.getUserId())) {
			lambda.eq(PointDetail::getUserId, param.getUserId());
		}
		if (!StringUtils.isEmpty(param.getNum())) {
			lambda.eq(PointDetail::getNum, param.getNum());
		}
		lambda.orderBy(true, false, PointDetail::getCreateTime);
	}

}
